﻿using System.Threading;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;
using System.Collections.Generic;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIWindowTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetVisualState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.AreEqual(WindowVisualState.Normal, window.WindowVisualState.GetValueOrDefault());
        }

        [TestMethod]
        public void ShouldGetInteractionState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.AreEqual(WindowInteractionState.ReadyForUserInteraction, window.WindowInteractionState.GetValueOrDefault());
        }

        [TestMethod]
        public void ShouldGetIsModal()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsFalse(window.IsModal);
        }

        [TestMethod]
        public void ShouldGetCanMaximize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsFalse(window.CanMaximize); 
        }

        [TestMethod]
        public void ShouldGetCanMinimize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsFalse(window.CanMinimize); 
        }

        [TestMethod]
        public void ShouldGetCanMove()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsTrue(window.CanMove);
        }

        [TestMethod]
        public void ShouldGetCanRotate()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsFalse(window.CanRotate);
        }

        [TestMethod]
        public void ShouldGetCanResize()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            Assert.IsFalse(window.CanResize);
        }

        [TestMethod]
        public void ShouldGetSlowWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.UIButton("@Name='Slow'").Press();
            var slowWindow = new UIWindow("@Name='QuickUIT Slow Window'");
            slowWindow.Close();
        }

        [TestMethod]
        public void ShouldMaximizeWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.UIButton("@Name='Test'").Press();
            var testWindow = new UIWindow("@Name='QuickUIT Test Window'");
            Assert.AreEqual(WindowVisualState.Normal, testWindow.WindowVisualState.GetValueOrDefault());
            testWindow.Maximize();
            Assert.AreEqual(WindowVisualState.Maximized, testWindow.WindowVisualState.GetValueOrDefault());
        }

        [TestMethod]
        public void ShouldMinimizeWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.UIButton("@Name='Test'").Press();
            var testWindow = new UIWindow("@Name='QuickUIT Test Window'");
            Assert.AreEqual(WindowVisualState.Normal, testWindow.WindowVisualState.GetValueOrDefault());
            testWindow.Minimize();
            Assert.AreEqual(WindowVisualState.Minimized, testWindow.WindowVisualState.GetValueOrDefault());
        }

        [TestMethod]
        public void ShouldWaitUntilReadyForUserInteraction()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.WaitUntilReadyForUserInteraction(1000);
        }

        [TestMethod]
        public void ShouldWaitUntilReady()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.WaitUntilReady(1000);
        }

        [TestMethod]
        public void ShouldWaitForInputIdle()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.WaitForInputIdle(1000);
        }

        [TestMethod]
        public void ShouldGetAllWindows()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIWindow> windows = window.UIWindows();
            Assert.AreEqual(0, windows.Count);
        }

        [TestMethod]
        public void ShouldMoveWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.Move(10, 10);
        }

        [TestMethod]
        public void ShouldResizeWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.UIButton("@Name='Test'").Press();
            Thread.Sleep(1000);
            var testWindow = new UIWindow("@Name='QuickUIT Test Window'");
            testWindow.Resize(100, 100);
        }

        [TestMethod]
        public void ShouldCloseWindow()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            window.UIButton("@Name='Test'").Press();
            var testWindow = new UIWindow("@Name='QuickUIT Test Window'");
            testWindow.Close(); 
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}